package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.model.TbTypeTemplate;
import com.dongdongshop.model.TbTypeTemplateExample;
import com.dongdongshop.page.PageParms;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TypeTemplateService;
import com.dongdongshop.vo.ItemCatVO;
import com.dongdongshop.vo.SpecificationOptionVO;
import com.dongdongshop.vo.SpecificationVO;
import com.dongdongshop.vo.TypeTemplateVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TypeTemplateServiceImpl implements TypeTemplateService {

    @Autowired
    private TbTypeTemplateMapper ttm;

    @Autowired
    private ItemCatService tcs;

    @Autowired
    private SpecificationService ss;

    @Override
    public PageResult<TypeTemplateVO> selectAll(PageParms<TypeTemplateVO> parms) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(parms.getParm().getName())) {
            criteria.andNameLike("%" + parms.getParm().getName() + "%");
        }
        PageHelper.startPage(parms.getPageNum(), parms.getPageSize());
        List<TbTypeTemplate> templatesList = ttm.selectByExample(example);
        PageInfo info = new PageInfo<>(templatesList);
        List<TypeTemplateVO> voList = templatesList.stream().map(tbTypeTemplate -> {
            TypeTemplateVO typeTemplateVO = new TypeTemplateVO();
            BeanUtils.copyProperties(tbTypeTemplate, typeTemplateVO);
            return typeTemplateVO;
        }).collect(Collectors.toList());
        return new PageResult<>(voList, info.getTotal(), parms.getPageNum(), parms.getPageSize());
    }

    @Override
    public void delete(Long[] ids) {
        TbTypeTemplateExample tbTypeTemplateExample = new TbTypeTemplateExample();
        tbTypeTemplateExample.createCriteria().andIdIn(Arrays.asList(ids));
        ttm.deleteByExample(tbTypeTemplateExample);
    }

    @Override
    public TypeTemplateVO toUpdate(Long id) {
        TbTypeTemplate tbTypeTemplate = ttm.selectByPrimaryKey(id);
        TypeTemplateVO templateVO = new TypeTemplateVO();
        BeanUtils.copyProperties(tbTypeTemplate, templateVO);
        return templateVO;
    }

    @Override
    public void update(TypeTemplateVO vo) {
        TbTypeTemplate tbTypeTemplate = new TbTypeTemplate();
        BeanUtils.copyProperties(vo, tbTypeTemplate);
        ttm.updateByPrimaryKeySelective(tbTypeTemplate);
    }

    @Override
    public void add(TypeTemplateVO vo) {
        TbTypeTemplate tbTypeTemplate = new TbTypeTemplate();
        BeanUtils.copyProperties(vo, tbTypeTemplate);
        ttm.insertSelective(tbTypeTemplate);
    }

    @Override
    public TypeTemplateVO getByItemCatId(Long itemCatId) {
        ItemCatVO catVO = tcs.tuUpdate(itemCatId);
        Long typeId = catVO.getTypeId();
        TbTypeTemplate tbTypeTemplate = ttm.selectByPrimaryKey(typeId);
        TypeTemplateVO templateVO = new TypeTemplateVO();
        //获取规格
        List<SpecificationVO> voList = JSONObject.parseArray(tbTypeTemplate.getSpecIds(), SpecificationVO.class);
        //获取规格选项
        List<SpecificationOptionVO> specList = ss.specLOptionist();
        //规格和规格选项匹配
        Map<Long, List<SpecificationOptionVO>> groupMap = specList.stream().collect(Collectors.groupingBy(o -> o.getSpecId()));
        for (SpecificationVO vo : voList) {
            List<SpecificationOptionVO> optionVOS = groupMap.get(vo.getId());
            if (optionVOS != null && !optionVOS.isEmpty()){
                vo.setSpecOptionList(optionVOS);
            }
        }
        templateVO.setSpecVOList(voList);
        BeanUtils.copyProperties(tbTypeTemplate, templateVO);
        return templateVO;
    }
}
